package com.ukao.pad.ui.packPackag;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.pad.R;
import com.ukao.pad.adapter.BottomPhotoAdapter;
import com.ukao.pad.base.BaseActivity;
import com.ukao.pad.bean.ProductRelImgBean;
import com.ukao.pad.listener.MyOnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PhotoActivity extends BaseActivity {


    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.left_btn)
    Button leftBtn;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private int pagerPosition;
    private ArrayList<String> urls;
    private BottomPhotoAdapter mBottomPhotoAdapter;
    private List<ProductRelImgBean>   mBottomData=new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        ButterKnife.bind(this);
        initView();
        initAdapter();
        initListener();

    }


    @Override
    protected void initView() {
        urls = getIntent().getStringArrayListExtra(ARG_PARAM1);
        pagerPosition = getIntent().getIntExtra(ARG_PARAM2, 0);// 获得位置
        ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(urls.size() - 1);
        mViewPager.setCurrentItem(pagerPosition, false);
        setStateButton();



    }

    private void initAdapter() {
        for (int i = 0; i < urls.size(); i++) {
            ProductRelImgBean mProductRelImgBean=new ProductRelImgBean(urls.get(i),pagerPosition==i?true:false);
            mBottomData.add(mProductRelImgBean);
        }

        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mBottomPhotoAdapter= new BottomPhotoAdapter(this,mBottomData);
        recyclerView.setAdapter(mBottomPhotoAdapter);

    }

    @Override
    protected void initListener() {
        super.initListener();
        mViewPager.addOnPageChangeListener(mOnPageChange);
        mBottomPhotoAdapter.setOnItemClickListener(mOnItemClickListener);
    }

    private OnItemClickListener mOnItemClickListener=new MyOnItemClickListener(){

        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int pos) {
            super.onItemClick(viewGroup, view, o, pos);
            setBottomSelect(pos,true );
        }
    };

    private void setBottomSelect(int pos,boolean isSetCurrentItem) {
        ProductRelImgBean mitemLever =mBottomPhotoAdapter.getItem(pos);
        if (!mitemLever.isCheck()) {
            mitemLever.setCheck(!mitemLever.isCheck());
            List<ProductRelImgBean> mData= mBottomPhotoAdapter.getDatas();
            for (int j = 0; j < mData.size(); j++) {
                if (pos != j) {
                    mData.get(j).setCheck(false);

                }
            }
            if (isSetCurrentItem){
                pagerPosition=pos;
                mViewPager.setCurrentItem(pagerPosition, false);
            }
            mBottomPhotoAdapter.notifyDataSetChanged();
        }
    }


    private ViewPager.OnPageChangeListener mOnPageChange = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrollStateChanged(int pos) {



        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int pos) {
            pagerPosition=pos;
            setStateButton();
            setBottomSelect(pos,false );

        }
    };

    private void setStateButton() {
        if (urls.size()==1){
            leftBtn.setEnabled(false);
            rightBtn.setEnabled(false);
            leftBtn.setBackgroundResource(R.drawable.photo_enable_left);
            rightBtn.setBackgroundResource(R.drawable.photo_enable_right);
            return;
    }
        if(pagerPosition==0){
            leftBtn.setEnabled(false);
            rightBtn.setEnabled(true);
            leftBtn.setBackgroundResource(R.drawable.photo_enable_left);
        }else if (pagerPosition==urls.size()-1){
            rightBtn.setEnabled(false);
            leftBtn.setEnabled(true);
            leftBtn.setBackgroundResource(R.drawable.shape_phnoto_left_select);
            rightBtn.setBackgroundResource(R.drawable.photo_enable_right);
        }else {
            rightBtn.setEnabled(true);
            leftBtn.setEnabled(true);
            rightBtn.setBackgroundResource(R.drawable.shape_phnoto_right_select);
            leftBtn.setBackgroundResource(R.drawable.shape_phnoto_left_select);
        }
    }

    @OnClick({R.id.left_btn, R.id.right_btn,R.id.back_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_btn:
                if (pagerPosition==0){
                    return;
                }
                pagerPosition=pagerPosition-1;
                mViewPager.setCurrentItem(pagerPosition, false);
                break;
            case R.id.right_btn:
                if (pagerPosition==urls.size()-1){
                    return;
                }
                pagerPosition=pagerPosition+1;
                mViewPager.setCurrentItem(pagerPosition, false);
                break;
            case  R.id.back_btn:
              finish();
                break;
        }
    }


    private class ImagePagerAdapter extends FragmentPagerAdapter {

        public ArrayList<String> fileList;

        public ImagePagerAdapter(FragmentManager fm, ArrayList<String> fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.size();
        }


        @Override
        public Fragment getItem(int position) {
            String url = fileList.get(position);
            return PhotoDetailFragment.newInstance(url);
        }

    }
}
